﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Role.Entity;
namespace MvcMasterPage.Controllers
{
    public class WorkFlowController : Controller
    {
        Role.Data.WorkFlow_InfoRepository repository;
        public WorkFlowController()
        {
            repository = new Role.Data.WorkFlow_InfoRepository();
        }

        public ActionResult Index()
        {
            var model = repository.GetDetailModel().OrderBy(i => i.WorkFlowID).ThenBy(i => i.PrevNode ?? 0).ToList();
            return View(model);
        }
        public ActionResult Edit(int? id)
        {
            var model = repository.GetEntity<WorkFlow_Info>(i => i.ID == id.Value);
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(WorkFlow_Info entity)
        {
            if (ModelState.IsValid)
            {
                repository.Update<WorkFlow_Info>(i => new WorkFlow_Info
                {
                    ID = entity.ID,
                    Name = entity.Name,
                    NextNode = entity.NextNode,
                    PrevNode = entity.PrevNode,
                });
                return RedirectToAction("Index");
            }
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(WorkFlow_Info entity)
        {
            if (ModelState.IsValid)
            {
                repository.Insert(entity);
                return RedirectToAction("Index");
            }
            return View();
        }

        public JsonResult Save()
        {
            var js = new JsonResult();
            string[] idArr = Request.Form["id"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] prevArr = Request.Form["PrevNode"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] nextArr = Request.Form["NextNode"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            List<WorkFlow_Info> workFlow_InfoList = new List<WorkFlow_Info>();
            for (int i = 0; i < idArr.Length; i++)
            {
                workFlow_InfoList.Add(new WorkFlow_Info
                {

                });
            }
            return js;
        }
    }
}
